using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using _7tc056_QuanLyNhaSach.HeThongXuLy;
using _7tc056_QuanLyNhaSach.HeThongLuuTru;
using System.Windows.Forms;

namespace _7tc056_QuanLyNhaSach.HeThongLuuTru
{
    class PhieuNhapDAO
    {
        
        public static DataTable PHIEUNHAP()
        {
            string sql= "select * from PHIEUNHAP";
            return DataAssecc.ExecQuery(sql);
        }
        static public DataTable SelectPhieuNhapAll()
        {
            string sql = "select* from PHIEUNHAP";
            DataTable dt = DataAssecc.ExecQuery(sql);
            return dt;
        }

        static public DataTable SelectChiTietPhieuNhapAll()
        {
            string sql = "select * from CT_PHIEUNHAP";
            DataTable dt = DataAssecc.ExecQuery(sql);
            return dt;
        }
        static public DataTable SelectChiTietSach()
        {
            string sql = "select MaSach,TenSach,SoLuong from SACH";
            DataTable dt = DataAssecc.ExecQuery(sql);
            return dt;
        }
        static public void Insert(PhieuNhapDTO p)
        {
            string sql = "insert into CT_PHIEUNHAP(MaPhieuNhap,MaSach,TenSach,SoLuong,GiaNhap) values(" + p.MaPhieuNhap + "," + p.MaSach + ",'" + p.TenSach + "'," + p.SoLuong + "," + p.GiaNhap + ")";
            DataAssecc.ExecNonQuery(sql);
        }

        static public bool Insertphieunhap(PhieuNhapDTO p)
        {
            try
            {
                string sql = "insert into PHIEUNHAP(NgayNhap) values('" + p.NgayNhap + "')";
                DataAssecc.ExecNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        public static PhieuNhapDTO GetPhieuNhapByName(int maphieunhap, int masach)
        {
            string sql = "select * from CT_PHIEUNHAP where ((MaPhieuNhap=" + maphieunhap + ")AND(MaSach = " + masach + "))";
            DataTable dt = DataAssecc.ExecQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                PhieuNhapDTO pn = new PhieuNhapDTO();
                pn.MaPhieuNhap = (int)dt.Rows[0].ItemArray[0];
                return pn;
            }

        }
        public static bool themphieunhap(PhieuNhapDTO p)
        {
            PhieuNhapDAO.Insertphieunhap(p);
            return true;
        }

        public static bool themchitietphieunhap(PhieuNhapDTO p)
        {
            if (PhieuNhapDAO.GetPhieuNhapByName(p.MaPhieuNhap, p.MaSach) == null)
            {
                PhieuNhapDAO.Insert(p);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool XoaPhieuNhap(PhieuNhapDTO pn, ChiTietPhieuNhapDTO pct)
        {
            try
            {
                //string sql = "Delete from CT_PHIEUNHAP where CT_PhieuNhap.maphieunhap = " + pn.MaPhieuNhap;
             string sql = "Delete from CT_PHIEUNHAP, PHIEUNHAP where phieunhap.maphieunhap = " + pn.MaPhieuNhap + " and ct_phieunhap.maphieunhap= " + pct.MaPhieuNhap;
                DataAssecc.ExecNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

      
    }

}

